﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Core
{
    /// <summary>
    /// Represents a lazily evaluated value. The Lazy is created with a function to evaluate - the first time the value is requested the function is executed 
    /// and the value cached for all subsequent access to the value
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Lazy<T>
    {
        private Func<T> _evalFunc;
        private T _value;
        private bool _evaluated;

        public Lazy(Func<T> evalFunc)
        {
            evalFunc.ThrowIfNull("evalFunc");
            this._evalFunc = evalFunc;
        }

        public T Value
        {
            get
            {
                if (!this._evaluated)
                {
                    this._value = this._evalFunc();
                    this._evaluated = true;
                }

                return this._value;
            }
        }
    }
}
